package demo.collections;

import java.util.HashSet;
import java.util.Iterator;

@SuppressWarnings("unused")
public class SetTT {
	public static void main(String[] args) {
		method3();
	}

	private static void method3() {
		HashSet<Person> hs = new HashSet<Person>();
		Person p1 = new Person("1");
		Person p2 = new Person("2");
		Person p3 = new Person("2");
		System.out.println(p1);
		System.out.println(p2);
		System.out.println(p3);
		hs.add(p1);
		hs.add(p2);
		hs.add(p3);
		Iterator<Person> it = hs.iterator();
		while (it.hasNext())
			System.out.println(it.next());
	}

	private static void method2() {
		Node a = new Node(3);
		for (int i = 0; i < 30; i++)
			a.add((int) (Math.random() * 100 + 1));
		a.leftTra();
		System.out.println();
		a.rightTra();
	}

	// ����10��1~20֮��Ĳ��ظ��������
	private static void method1() {
		HashSet<Integer> set = new HashSet<Integer>();
		while (set.size() < 10)
			set.add((int) (Math.random() * 20 + 1));
		System.out.println(set);
	}
}

//�Զ��������
class Node {
	Node left = null;
	Node right = null;
	int value;

	public Node(int a) {
		value = a;
	}

	public void add(int a) {
		if (value == a)
			return;
		else if (value > a) {
			if (left != null)
				left.add(a);
			else
				left = new Node(a);
		} else {
			if (right != null)
				right.add(a);
			else
				right = new Node(a);
		}
	}

	// �����
	public void leftTra() {
		if (left != null)
			left.leftTra();
		System.out.print(value + ",");
		if (right != null)
			right.leftTra();
	}

	// �ұ���
	public void rightTra() {
		if (right != null)
			right.rightTra();
		System.out.print(value + ",");
		if (left != null)
			left.rightTra();
	}
}

class Person {
	String name;

	@Override
	public int hashCode() {
		System.out.println("hashcodeִ����");
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		System.out.println("equalsִ����");
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Person other = (Person) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

	Person(String name) {
		this.name = name;
	}
}
